package com.dlwx.parttimetalent.utils;

import android.content.Context;
import android.util.Log;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.NetUtils;

import static com.lzy.okgo.utils.HttpUtils.runOnUiThread;

/**
 * Created by Administrator on 2017/10/8.
 */

public class HxUtils {
    private Context context;
    private static final  String psw="123456";

    public HxUtils(Context context) {
        this.context = context;
    }
//注册
    public static void register(final String name){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().createAccount(name, psw);
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
//                    login(name);

                }
            }).start();
    }
//    登录
    public static void login(final String name){
//        即首次登录成功后，不需要再次调用登录方法，在下次 APP 启动时，SDK 会自动为您登录。
        EMClient.getInstance().login(name,psw,new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                Log.d("main", "登录聊天服务器成功！");
            }

            @Override
            public void onProgress(int progress, String status) {

            }
            @Override
            public void onError(int code, String message) {
                Log.d("main", message+"登录聊天服务器失败！"+code);
                if(code==204){
//                    register(name);
                }else{

                }
            }
        });
    }
//    退出
    public static void loginOut(){
//        同步
//        int code=EMClient.getInstance().logout(true);
//        LogUtils.i("环信退出登录...."+code);
//        异步
        EMClient.getInstance().logout(true, new EMCallBack() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                LogUtils.i("环信退出登录成功");

            }

            @Override
            public void onProgress(int progress, String status) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(int code, String message) {
                // TODO Auto-generated method stub
                LogUtils.i("环信退出登录失败"+code+"-msg:"+message);
                if(code==212){
                   int c= EMClient.getInstance().logout(true);
                    LogUtils.i("环信退出登录失败-c:"+c);
                }
            }
        });
    }



        //注册一个监听连接状态的listener
    public void addListener(){
        EMClient.getInstance().addConnectionListener(new MyConnectionListener());
    }

        //实现ConnectionListener接口
        private class MyConnectionListener implements EMConnectionListener {
            @Override
            public void onConnected() {
            }
            @Override
            public void onDisconnected(final int error) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if(error == EMError.USER_REMOVED){
                            // 显示帐号已经被移除
                        }else if (error == EMError.USER_LOGIN_ANOTHER_DEVICE) {
                            // 显示帐号在其他设备登录
                        } else {
                            if (NetUtils.hasNetwork(context)){}
                            //连接不到聊天服务器
                    else{}
                            //当前网络不可用，请检查网络设置
                        }
                    }
                });
            }
        }
}
